<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

/**
 * 后台内容控制器
 * 
 * @author huajie <banhuajie@163.com>
 */
class APPVersionController extends AdminController
{

    public function index($pid = 0) {
        $M_app_version = M('app_version');        
       
        $list = $this->lists("app_version");        
        int_to_string($list,array('mask'=>array(0=>'否',1=>'是')));
        $this->assign('_list', $list);
        $this->meta_title = "app版本列表";
        $this->display();
    }
    
    public function add($pid = 0){
        $M_app_version = M('app_version');    
       
        if (IS_POST) {
            $data = params_request(array('title', 'hash', 'url','os', 'description','mask','number'));
            
            !preg_match('/^.{0,100}$/', $data['title']) && $this->error('标题长度必须在4~60个字符');
            
            $data['create_time'] = NOW_TIME;
            $data['update_time'] = NOW_TIME;
            $data['uid']=UID;
            $id = $M_app_version->add($data);
            
            $id > 0 ? $this->success('新增成功', U('index')) : $this->error('新增失败，未知错误');
        } else {
          
            $this->meta_title = '新增版本';
            $this->display('edit');
        }
    }

    public function edit(){
        $id = I('request.id');
        
        $M_app_version = M('app_version');
        
        $info = $M_app_version->find($id);
        !$info && $this->error('非法参数');       
        
        if (IS_POST) {
            $data = params_request(array('id', 'title', 'hash', 'url', 'description','mask','number'));
            
            
            !preg_match('/[\x{4e00}-\x{9fa5}A-Za-z0-9_-]{3,60}+$/u', $data['title']) && $this->error('标题长度必须在4~60个字符');
            
            $data['update_time'] = NOW_TIME;            
            $status = $M_app_version->where(array('id'=>$id))->save($data);
            
            $status ? $this->success('编辑成功', U('index')) : $this->error('编辑失败，未知错误');
        } else {
            $this->assign('info', $info);
            $this->meta_title = '编辑版本章';
            $this->display('edit');
        }
    }

    public function delete() {
        $ids = I('id');       
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        
        $M_app_version = M('app_version');        
        $map    =   array(); 
        is_array($ids) && $ids=implode(',', $ids);
        $map['id'] = array('in', $ids);    
      
        $M_app_version->where($map)->delete()!==false ? $this->success('删除成功！','',IS_AJAX) : $this->error('删除失败！','',IS_AJAX);
        
    }
}
